//
//  ViewController.m
//  UI19 - Car(汽车之家)
//
//  Created by 韩旭 on 16/8/9.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "ViewController.h"
#import "HX_CustomCell.h"
#import "TitleModel.h"
#import "CarModel.h"
#import "UIImageView+WebCache.h"
#import "HX_CutomCollectionCell.h"
#import "HotModel.h"
#import "HX_FlagCarCell.h"
#import "FlagshipModel.h"
#import "HX_TitleView.h"
#import "HX_TitleFlag.h"
#define kTableView @"tableView"
#define kCollection @"collection"
#define kCollectionHead @"head"
#define kCollectionHot @"hot"
#define kCollectionFlag @"flag"
@interface ViewController () <UITableViewDataSource, UITableViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) NSMutableArray *arrDataSource;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *viewBack;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *arrCollection;
@property (nonatomic, strong) NSMutableArray *arrFlag;
@end

@implementation ViewController

- (NSMutableArray *)arrDataSource {
    if (!_arrDataSource) {
        _arrDataSource = [NSMutableArray array];
    }
    return _arrDataSource;
}

- (NSMutableArray *)arrCollection {
    if (!_arrCollection) {
        _arrCollection = [NSMutableArray array];
    }
    return _arrCollection;
}
- (NSMutableArray *)arrFlag {
    if (!_arrFlag) {
        _arrFlag = [NSMutableArray array];
    }
    return _arrFlag;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"汽车之家";
    self.arrDataSource = [[TitleModel groupArray] mutableCopy];
    self.arrCollection = [[HotModel groupsArray] mutableCopy];
    self.arrFlag = [[FlagshipModel groupsArrAll] mutableCopy];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self creatTitleView];
    [self creatTableView];
}
- (void)creatTitleView
{
    self.viewBack = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 450)];
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.minimumInteritemSpacing = 5;
    layout.minimumLineSpacing = 5;
    layout.sectionInset = UIEdgeInsetsMake(0, 15, 0, 40);
    layout.sectionHeadersPinToVisibleBounds = YES;
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kWidth, _viewBack.frame.size.height) collectionViewLayout:layout];
    [self.viewBack addSubview:_collectionView];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    layout.headerReferenceSize = CGSizeMake(0, 40);
    [_collectionView registerClass:[HX_CutomCollectionCell class] forCellWithReuseIdentifier:kCollection];
    [_collectionView registerClass:[HX_FlagCarCell class] forCellWithReuseIdentifier:kCollectionHot];
    [self.view addSubview:_viewBack];
    [_collectionView registerClass:[HX_TitleView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionHead];
    [_collectionView registerClass:[HX_TitleFlag class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader  withReuseIdentifier:kCollectionFlag];
}
- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight) style:0];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView registerClass:[HX_CustomCell class] forCellReuseIdentifier:kTableView];
    self.tableView.tableHeaderView = _viewBack;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return _arrCollection.count;
    } else {
        return _arrFlag.count;
    }
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        HX_CutomCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollection forIndexPath:indexPath];
        cell.hotModel = _arrCollection[indexPath.item];
        return cell;
    } else {
        HX_FlagCarCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionHot forIndexPath:indexPath];
        cell.flagModel = _arrFlag[indexPath.item];
        return cell;
    }
    
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        HX_TitleView *cell = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionHead forIndexPath:indexPath];
        cell.lable.text = @"热门品牌";
        return cell;
    } else {
        HX_TitleFlag *flag = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionFlag forIndexPath:indexPath];
        flag.lable.text = @"主打车";
        return flag;
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return CGSizeMake((kWidth - 100) / 5, 100);
    } else {
        return CGSizeMake((kWidth - 100) / 3, 150);
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return _arrDataSource.count + 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else if (section == 1){
        return 1;
    } else {
        TitleModel *model = _arrDataSource[section - 2];
        return model.list.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:3 reuseIdentifier:@"sd"];
        return cell;
    } else if (indexPath.section == 1) {
        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:3 reuseIdentifier:@"fsd"];
        return cell;
    } else {
        HX_CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableView];
        TitleModel *model = _arrDataSource[indexPath.section - 2];
        cell.model = model.list[indexPath.row];
        return cell;
    }
}
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    NSMutableArray *arrTitle = [NSMutableArray array];
    for (TitleModel *model in _arrDataSource) {
        [arrTitle addObject:model.letter];
    }
    NSString *string = @"热主选";
    [arrTitle insertObject:string atIndex:0];
    return arrTitle;
    
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return @"fdsf";
    } else if (section == 1) {
        return @"fd";
    } else {
return [self.arrDataSource[section - 2] letter];
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
